﻿namespace MicroCloud.Utils
{
    /// <summary>
    /// 相等比较辅助类，用于快速创建<see cref="IEqualityComparer{T}"/>的实例
    /// </summary>
    /// <example>
    /// var equalityComparer1 = EqualityHelper[Person].CreateComparer(p => p.ID);
    /// var equalityComparer2 = EqualityHelper[Person].CreateComparer(p => p.Name);
    /// var equalityComparer3 = EqualityHelper[Person].CreateComparer(p => p.Birthday.Year);
    /// </example>
    /// <typeparam name="T"> </typeparam>
    public static class EqualityHelper<T>
    {
        /// <summary>
        /// 创建指定对比委托<paramref name="keySelector"/>的实例
        /// </summary>
        /// <typeparam name="TV"></typeparam>
        /// <param name="keySelector"></param>
        /// <returns></returns>
        public static IEqualityComparer<T> CreateComparer<TV>(Func<T, TV> keySelector)
        {
            return new CommonEqualityComparer<TV>(keySelector);
        }
        
        /// <summary>
        /// 创建指定对比委托<paramref name="keySelector"/>与结果二次比较器<paramref name="comparer"/>的实例
        /// </summary>
        /// <typeparam name="TV"></typeparam>
        /// <param name="keySelector"></param>
        /// <param name="comparer"></param>
        /// <returns></returns>
        public static IEqualityComparer<T> CreateComparer<TV>(Func<T, TV> keySelector, IEqualityComparer<TV> comparer)
        {
            return new CommonEqualityComparer<TV>(keySelector, comparer);
        }


        /// <summary>
        /// 通用均衡器
        /// </summary>
        /// <typeparam name="TV"></typeparam>
        private class CommonEqualityComparer<TV> : IEqualityComparer<T>
        {
            //字段
            private readonly IEqualityComparer<TV> _comparer;
            private readonly Func<T, TV> _keySelector;

            #region 构造函数
            /// <summary>
            /// 初始化一个通用均衡器 <see cref="CommonEqualityComparer{TV}"/> 的新实例
            /// </summary>
            /// <param name="keySelector"></param>
            /// <param name="comparer"></param>
            public CommonEqualityComparer(Func<T, TV> keySelector, IEqualityComparer<TV> comparer)
            {
                _keySelector = keySelector;
                _comparer = comparer;
            }
            
            /// <summary>
            /// 初始化一个通用均衡器 <see cref="CommonEqualityComparer{TV}"/> 的新实例
            /// </summary>
            /// <param name="keySelector"></param>
            public CommonEqualityComparer(Func<T, TV> keySelector)
                : this(keySelector, EqualityComparer<TV>.Default)
            { }
            #endregion

            #region 方法
            /// <summary>
            /// 确定指定的对象是否相等
            /// </summary>
            /// <param name="x">要比较的第一个T类型的对象</param>
            /// <param name="y">要比较的第二个T类型的对象</param>
            /// <returns></returns>
            public bool Equals(T x, T y)
            {
                return _comparer.Equals(_keySelector(x), _keySelector(y));
            }
            
            /// <summary>
            /// 返回指定对象的哈希代码
            /// </summary>
            /// <param name="obj">要为其返回哈希代码的System.Object</param>
            /// <returns>指定对象的哈希代码</returns>
            public int GetHashCode(T obj)
            {
                return _comparer.GetHashCode(_keySelector(obj));
            }

            #endregion

        }

    }
}